<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\OrderComment as OrderCommentModel;
use app\common\model\Order as OrderModel;
use app\common\model\Goods as GoodsModel;

/**
 * OrderComment 商品评论控制器
 * Class OrderComment
 * @package app\home\controller
 */
class OrderComment extends Base
{
    /**
     * [getIndex  获取评论]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new OrderCommentModel;
        $gmodel = new GoodsModel;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['o.goodsid'] = $param['goodsid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size,'m.username,m.img,g.person_num option_personnum,g.day option_day,c.create_time,c.content,c.grade,o.goodsid');
            foreach($lists as &$v){
                $goods = $gmodel->findData($v['goodsid'],'id','title');
                $v['goods_title'] = $goods['title'];
                $http = substr($v['img'],0,4);
                if($http != 'http') {
                    $v['img'] = config('domain_name') . $v['img'];
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getAllcomment  获取总评]
     * @return json
     * @author [C。]
     */
    public function getAllcomment()
    {
        $param = input('param.');
        $model = new OrderCommentModel;
        try{
            $map['o.goodsid'] = $param['goodsid'];
            $allgrade = $model->getAllSum($map,'grade');
            $allcount = $model->getAllCounts($map);
            if($allcount == 0){
                $data = 0;
            }else{
                $data = $allgrade/$allcount;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',round($data,2));
    }
    /**
     * [getAdd  添加评论]
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new OrderCommentModel();
        $omodel = new OrderModel;
        $user = $this->user;
        try{
            $order = $omodel->findData($param['orderid'],'id','status,gotime');
            $comment = $model->findData($param['orderid'],'orderid');
            if(!empty($comment)){
                return admin_json(-1, '该订单已评价！');
            }
            $day = strtotime(date('Y-m-d'));
            if($order['status'] !== 1 || $day < $order['gotime']){
                return admin_json(-1, '该订单未完成，无法评价！');
            }
            $param['memid'] = $user['id'];
            $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
}